﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Collections;
namespace GPCSC.Web
{
    /// <summary>
    /// fileupload 的摘要说明
    /// </summary>
    /// 
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class fileupload : IHttpHandler
    {

        /// <summary>
        /// UPLoadHandler 的摘要说明
        /// </summary>
        ///     /// <summary>
        /// $codebehindclassname$ 的摘要说明
        /// </summary>

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";
            HttpPostedFile file = context.Request.Files["Filedata"];
            string uploadPath =
                HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";

            if (file != null)
            {
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                string type = file.FileName;
                string typetr = type.Substring(type.LastIndexOf('.') + 1);
                string typrs = file.ContentType;
                string postid = context.Request.QueryString["postid"].ToString();

                #region  查找部门名称

                Hashtable htable = new Hashtable();
                htable.Add("@POSTID", postid);
                 GPCSC.Web.SqlReadXml sdf = new SqlReadXml();
                 DataSet ds = sdf.ReaderDataSet("sel_post_one", htable);


                #endregion
                 string postname = ds.Tables[0].Rows[0]["POSTNAME"].ToString();
               
                string year = DateTime.Now.Year.ToString();
                string month = DateTime.Now.Month.ToString();
                string filenames = postname+year+"年"+month+"月统计表"+"."+typetr;

                file.SaveAs(uploadPath+filenames);
                //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
               // string filesid = context.Request.QueryString["newsid"];
               //AfterFileSave();
                AfterFileSave(filenames, postid,postname);
                context.Response.Write("1");

                //上传文件完成之后

            }
            else
            {
                context.Response.Write("0");
            }  
        }
        protected void AfterFileSave(string filenames,string postid,string postname)
        {
            Hashtable htable = new Hashtable();
            string FileID = Guid.NewGuid().ToString();
            string tjdate = DateTime.Now.ToString();
            string year  = DateTime.Now.Year.ToString();
            string month = DateTime.Now.Month.ToString();
            string filename = filenames;
            string fileUrl = "uploadfile/" + filenames;
            string POstID = postid;
            string PostName = postname;
            htable.Add("FileID", FileID);
            htable.Add("tjdate", tjdate);
            htable.Add("filename", filenames);
            htable.Add("fileUrl", fileUrl);
            htable.Add("year",year);
            htable.Add("month", month);
            htable.Add("POstID", postid);
            htable.Add("PostName", PostName);
            SQLServerDAL.databyhtable dtable = new SQLServerDAL.databyhtable();
            dtable.databyhtables(htable, "add", "TJfile","","");

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}